package GoDBusDemoClient

import (
	"fmt"
	"log"
	"time"

	tdbus "github.com/godbus/dbus"
)

type GoDbusDemoClientClass struct {
	m_sysBus   *tdbus.Conn
	m_signalCh chan *tdbus.Signal
}

func (client *GoDbusDemoClientClass) ClientTest() {

	sessionDus, err := tdbus.SessionBus()
	if err != nil {
		fmt.Printf("get session dbus failed: %s \n", err)
		return
	}

	// 调用dbus接口获取字符数组
	ddeNetDBusObj := sessionDus.Object("com.deepin.daemon.Network", "/com/deepin/daemon/Network")
	// 返回参数
	var supportedNetConnTypes []string
	err = ddeNetDBusObj.Call("com.deepin.daemon.Network.GetSupportedConnectionTypes", tdbus.Flags(tdbus.TypeMethodReply)).Store(&supportedNetConnTypes)
	if err != nil {
		fmt.Printf("get session dbus failed: %s \n", err)
		return
	}

	for i, connType := range supportedNetConnTypes {
		fmt.Printf("supportedNetConnType[%v]: %s \n", i, connType)
	}

	// 调用dbus接口获取map数据
	ddeDisplayDBusObj := sessionDus.Object("com.deepin.daemon.Display", "/com/deepin/daemon/Display")
	// 返回参数
	var brightnessInfoMap map[string]float64
	err = ddeDisplayDBusObj.Call("com.deepin.daemon.Display.GetBrightness", tdbus.Flags(tdbus.TypeMethodReply)).Store(&brightnessInfoMap)
	if err != nil {
		fmt.Printf("get session dbus failed: %s \n", err)
		return
	}

	for key, value := range brightnessInfoMap {
		fmt.Printf("brightnessInfo: %s %v\n", key, value)
	}

	// 调用dbus接口获取属性
	ddeAudioDBusObj := sessionDus.Object("com.deepin.daemon.Audio", "/com/deepin/daemon/Audio")
	// 返回参数
	var audioSourceObjectpathsVar tdbus.Variant
	err = ddeAudioDBusObj.Call("org.freedesktop.DBus.Properties.Get", tdbus.Flags(tdbus.TypeMethodReply), "com.deepin.daemon.Audio", "Sources").Store(&audioSourceObjectpathsVar)
	if err != nil {
		fmt.Printf("session dbus called failed: %s \n", err)
		return
	}

	audioSourceObjectpaths := audioSourceObjectpathsVar.Value().([]tdbus.ObjectPath)
	for i, objPath := range audioSourceObjectpaths {
		fmt.Printf("audioSourceObjectpath[%v]: %s\n", i, objPath)
	}

	// 调用dbus接口设置属性
	isIncreaseVolumeVar := tdbus.MakeVariant(false)
	err = ddeAudioDBusObj.Call("org.freedesktop.DBus.Properties.Set",
		tdbus.Flags(tdbus.TypeMethodReply),
		"com.deepin.daemon.Audio",
		"IncreaseVolume", isIncreaseVolumeVar).Store()

	if err != nil {
		fmt.Printf("session dbus called failed: %s \n", err)
		return
	}

	//// 连接dbus信号
	client.m_sysBus, err = tdbus.SystemBus()
	if err != nil {
		fmt.Printf("get sys dbus failed: %s \n", err)
		return
	}

	err = client.m_sysBus.BusObject().AddMatchSignal("org.freedesktop.DBus.ObjectManager", "InterfacesAdded", tdbus.WithMatchObjectPath("/org/freedesktop/UDisks2")).Err
	if err != nil {
		fmt.Printf("sys dbus signal add match failed: %s \n", err)
		return
	}

	// 创建信号通道
	client.m_signalCh = make(chan *tdbus.Signal, 10)
	client.m_sysBus.Signal(client.m_signalCh)
	// 线程监控信号
	go func() {
		for {
			select {
			case sig := <-client.m_signalCh:
				// log.Printf("sig: %#v\n", sig)
				if sig.Path == "/org/freedesktop/UDisks2" &&
					sig.Name == "org.freedesktop.DBus.ObjectManager.InterfacesAdded" {
					log.Printf("InterfacesAdded \n")
					var path tdbus.ObjectPath
					var stringVarMapMap map[string]map[string]tdbus.Variant
					err = tdbus.Store(sig.Body, &path, &stringVarMapMap)
					if err != nil {
						log.Printf("%s data store failed: %s", sig.Name, err)
					}

					// 打印添加的dbus路径
					log.Printf("added ifc's path: %s", path)
					for key, value := range stringVarMapMap {
						// 打印添加的dbus接口
						log.Printf("%s's added ifc: %s", path, key)
						// 打印添加的dbus接口中有哪些属性
						for propsKey, _ := range value {
							log.Printf("%s has property key: %s", key, propsKey)
						}

					}

				}
			}
		}
	}()

}

func (client *GoDbusDemoClientClass) StopClientTest() {
	// 退出信号监听
	time.Sleep(2 * time.Second)
	err := client.m_sysBus.BusObject().RemoveMatchSignal("org.freedesktop.DBus.ObjectManager", "InterfacesAdded", tdbus.WithMatchObjectPath("/org/freedesktop/UDisks2")).Err
	if err != nil {
		fmt.Printf("sys dbus signal remove match failed: %s \n", err)
		return
	}
	client.m_sysBus.RemoveSignal(client.m_signalCh)
}
